
// Configs
var x_jsDebugMode= true;
var x_jsAjaxTimeout= 2000;
var x_jsAjaxEvalSeparation= '<&javascript&>';
var x_jsAjaxConnectionLostTitle= "ERROR";
var x_jsAjaxConnectionLostText= "The connection was lost. <br />You may refresh the page or wait until the connection is back.";
var x_jsAjaxConnectionLostWarning= true;

var x_jsErrorMessage= function(){};
////////////////////////////////////////
// Connection Lost and Regain Callbacks
var x_jsAjaxConnectionLostCallback= function(title,text){
    var newConnectionLostDiv= document.createElement('div');
    newConnectionLostDiv.id= 'x_ajaxConnectionLostDiv';
    newConnectionLostDiv.style.zIndex= '1000';
    newConnectionLostDiv.innerHTML= '<div><div>'+((title!=undefined)? title:x_jsAjaxConnectionLostTitle)+'</div><span>'+((text!=undefined)? text:x_jsAjaxConnectionLostText)+'</span></div>';
    document.body.appendChild(newConnectionLostDiv);
};
var x_jsAjaxConnectionLostRegainCallback= function(){
    document.body.removeChild(document.getElementById('x_ajaxConnectionLostDiv'));
}///////////////////////////////
///////////////////////////////

var x_jsAjaxConnectionLost= false;
var x_jsAjaxWaitingForResponse= false;
var x_jsAjaxRegainConnectionCallback= function(){
    x_jsAjaxConnectionLost= false;
    if(document.getElementById('x_ajaxConnectionLostDiv')!=null){
        x_jsAjaxConnectionLostRegainCallback();
    }
};
var x_jsAjaxConnectionLostTimer= null;
var x_jsAjaxConnectionTimeout= null;
var x_jsAjaxConnectionErrorCallback= function(url,vars,callback,loading){
    if(x_jsAjaxConnectionLost==false){
        x_jsAjaxConnectionLostTimer= setTimeout(function(){x_jsAjaxConnectionLostCallback();},x_jsAjaxTimeout);
    }
    x_jsAjaxConnectionLost= true;
    if(x_jsAjaxWaitingForResponse==false)setTimeout(function(){x_ajax(url,vars,callback,loading);},x_jsAjaxTimeout);
};
function x_callback(callback,params){
    try{
        if(window[callback] != undefined){
            return window[callback](params);
        }else{
            if(typeof(callback)=="function"){
                return callback(params);
            }else{
                if(typeof(callback)=="string" && callback != ""){
                    eval(callback);
                }else{
                    return callback;
                }
            }
        }
    }catch(e){
        if(x_jsDebugMode)x_jsDebugModeEval(e);
    }
}
var x_ajaxStartLoadingCallback= function(){
    document.body.style.cursor= 'wait';
};
var x_ajaxStopLoadingCallback= function(){
    document.body.style.cursor= 'default';
};
var x_jsDebugModeEval= function(e){
    alert(e);
};
function x_escapePost(str){
    if(str){
        str +='';
        str=str.replace(/%/gm,'%25');
        str=str.replace(/\+/gm,'%2B');
        str=str.replace(/&/gm,'%26');
        str=str.replace(/=/gm,'%3D');
        str=str.replace(/ /gm,'%20');
        str=str.replace(/\t/gm,'%09');
        str=str.replace(/\r\n/gm,'%0D');
        str=str.replace(/\r/gm,'%0D');
        str=str.replace(/\n/gm,'%0D');
    }
    return str;
}
function xid(ID){
    if(document.getElementById(ID) != null){
        return document.getElementById(ID);
    }else{
        return document.createElement('textarea');
    }
}
var x_isLoading= false;
function x_ajaxStartLoading(){
    try{x_ajaxStartLoadingCallback();
    }finally{x_isLoading= true;}
}
function x_ajaxStopLoading(){
    try{x_ajaxStopLoadingCallback();
    }finally{x_isLoading= false;}
}
function x_ajax(url,vars,callback,loading){
    if(loading){
        x_ajaxStartLoading();
    }
    if (! url){
        url= window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1);
    }
    if (! url){
        url= window.location;
    }
    var method= "POST";
    var varsArray= vars;
    if(typeof(varsArray)=='object'){
        vars= "";
        for(var i in varsArray){
            vars += ((vars != "")? "&":"")+i+"="+x_escapePost(varsArray[i]);
        }
    }
    var ajaxRequest;
    try{ajaxRequest = new XMLHttpRequest(); }catch (e){
        try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}catch (e){
            try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}catch (e){
                return false;
            }
        }
    }
    if(callback){

        ajaxRequest.onoffline= function(){
            alert('Offline');
        }

        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 1){
                // Connection Initiated
                x_jsAjaxWaitingForResponse= true;
                if(x_jsAjaxConnectionLostWarning){
                    if(x_jsAjaxConnectionTimeout!=null)clearTimeout(x_jsAjaxConnectionTimeout);
                    x_jsAjaxConnectionTimeout= setTimeout(function(){x_jsAjaxConnectionErrorCallback(url,varsArray,callback,loading);},x_jsAjaxTimeout+((varsArray!=null && varsArray['action']=='stream')? x_jsAjaxStreamTimeout:0));
                }
            }
            if(ajaxRequest.readyState == 4){
                if(ajaxRequest.status == 200){
                    // Server Response Received
                    if(x_jsAjaxConnectionTimeout!=null)clearTimeout(x_jsAjaxConnectionTimeout);
                    if(x_jsAjaxConnectionLostWarning && x_jsAjaxConnectionLost){
                        if(x_jsAjaxConnectionLostTimer != null)clearTimeout(x_jsAjaxConnectionLostTimer);
                        x_jsAjaxRegainConnectionCallback();
                    }
                    x_callback(callback,ajaxRequest.responseText);
                    if(loading){
                        x_ajaxStopLoading();
                    }
                }else{
                    if(ajaxRequest.status == 0){
                        // Connection Error
                        x_jsAjaxWaitingForResponse= false;
                        if(x_jsAjaxConnectionLostWarning){
                            x_jsAjaxConnectionErrorCallback(url,varsArray,callback,loading);
                        }
                    }
                }
            }
        }
    }
    ajaxRequest.open(method, url, ((callback)? true : false));
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    ajaxRequest.setRequestHeader("X_REQUESTED_WITH", "xmlhttprequest");
    ajaxRequest.send(vars);
    if(!callback){
        if(loading)x_ajaxStopLoading();
        return ajaxRequest.responseText;
    }
}
function x_ajaxEval(data,container){
    var ajaxData= data.split(x_jsAjaxEvalSeparation);
    if(container != undefined && container != null){
        if(typeof(container)=='object'){
            container.innerHTML= ajaxData[0];
        }else{
            document.getElementById(container).innerHTML= ajaxData[0];
        }
    }
    x_safeEval(ajaxData[1]);
    return ajaxData[0];
}
function x_safeEval(data){
    if(data!=''){
        try{
            eval(data);
        }catch(e){
            if(x_jsDebugMode)x_jsDebugModeEval(e);
            else x_jsErrorMessage();
        }
    }
}
function x_loadAjax(url, params, container) {
    x_ajaxEval(x_ajax(url, params), container);
}
function x_formValuesToArray(formname){
    var elems= new Array();
    var elem = document.forms[formname].elements;
    for(var i = 0; i < elem.length; i++){
        if((elem[i].type!='radio' && elem[i].type!='checkbox') || ((elem[i].type=='radio' || elem[i].type=='checkbox') && elem[i].checked==true)){
            elems[elem[i].name]= elem[i].value;
        }
    }
    return elems;
}
function x_formValuesToString(formname){
    var elems= "";
    var elem = document.forms[formname].elements;
    for(var i = 0; i < elem.length; i++){
        if((elem[i].type!='radio' && elem[i].type!='checkbox') || ((elem[i].type=='radio' || elem[i].type=='checkbox') && elem[i].checked==true)){
            elems += ((elems=="")? "":"&") + x_escapePost(elem[i].name) + "=" + x_escapePost(elem[i].value);
        }
    }
    return elems;
}
